package com.payment.web.controller.system;

import com.payment.common.annotation.Log;
import com.payment.common.base.AjaxResult;
import com.payment.common.constant.Constants;
import com.payment.common.enums.BusinessType;
import com.payment.common.page.TableDataInfo;
import com.payment.common.utils.DateUtils;
import com.payment.common.utils.ExcelUtil;
import com.payment.common.utils.StringUtils;
import com.payment.common.utils.aliyun.OSSClientUtil;
import com.payment.common.utils.file.FileUtils;
import com.payment.framework.util.ShiroUtils;
import com.payment.framework.web.base.BaseController;
import com.payment.system.domain.ShoppingProduct;
import com.payment.system.service.IShoppingProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Random;

/**
 * 商品 信息操作处理
 *
 * @author payment
 * @date 2019-08-21
 */
@Controller
@RequestMapping("/system/shoppingProduct")
public class ShoppingProductController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ShoppingProductController.class);
    private String prefix = "system/shoppingProduct";

    @Autowired
    private IShoppingProductService shoppingProductService;

    @RequiresPermissions("system:shoppingProduct:view")
    @GetMapping()
    public String shoppingProduct() {
        return prefix + "/shoppingProduct";
    }

    /**
     * 查询商品列表
     */
    @RequiresPermissions("system:shoppingProduct:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShoppingProduct shoppingProduct) {
        startPage();
        List<ShoppingProduct> list = shoppingProductService.selectShoppingProductList(shoppingProduct);
        return getDataTable(list);
    }


    /**
     * 导出商品列表
     */
    @RequiresPermissions("system:shoppingProduct:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShoppingProduct shoppingProduct) {
        List<ShoppingProduct> list = shoppingProductService.selectShoppingProductList(shoppingProduct);
        ExcelUtil<ShoppingProduct> util = new ExcelUtil<ShoppingProduct>(ShoppingProduct.class);
        return util.exportExcel(list, "shoppingProduct");
    }

    /**
     * 新增商品
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品
     */
    @RequiresPermissions("system:shoppingProduct:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShoppingProduct shoppingProduct, MultipartFile[] fileImg) {
        try {
          /*  if(fileImg.length>1){
                return error("商品图片不能上传多张");
            }*/
            if (fileImg == null || fileImg.length == 0) {
                return error("请上传商品图片！");
            }
            if (shoppingProduct == null) {
                return error("获取商品信息失败，请联系后台管理人员！");
            }
            String fileUrl = "";
            for (int i = 0; i < fileImg.length; i++) {
                if (!FileUtils.fileType(fileImg[i])) {
                    return AjaxResult.error(1, "图片类型错误");
                }
                fileUrl += OSSClientUtil.uploadImg2Oss(fileImg[i], Constants.PRODUCT) + ";";
            }
            shoppingProduct.setImg(fileUrl.substring(0, fileUrl.length() - 1));
            shoppingProduct.setCreateName(ShiroUtils.getLoginName());
            shoppingProduct.setId(StringUtils.randomUUID());
            String number =  new Random().nextInt(10000)+"";
            shoppingProduct.setProductNumber( DateUtils.dateTimeNow()+number);
            return toAjax(shoppingProductService.insertShoppingProduct(shoppingProduct));
        } catch (Exception e) {
            LOGGER.error("保存商品失败！", e);
            return error("保存商品失败！");
        }
    }

    /**
     * 修改商品
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        ShoppingProduct shoppingProduct = shoppingProductService.selectShoppingProductById(id);
        mmap.put("shoppingProduct", shoppingProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品
     */
    @RequiresPermissions("system:shoppingProduct:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShoppingProduct shoppingProduct, MultipartFile[] fileImg) {
        try {
            if (shoppingProduct == null) {
                return error("获取商品信息失败，请联系后台管理人员！");
            }
            /*if(fileImg.length>1){
                return error("商品图片不能上传多张");
            }*/
            if (fileImg != null && fileImg.length > 0) {
                String fileUrl = "";
                for (int i = 0; i < fileImg.length; i++) {
                    if (!FileUtils.fileType(fileImg[i])) {
                        return AjaxResult.error(1, "图片类型错误");
                    }
                    //修改图片访问路径
                    fileUrl += OSSClientUtil.uploadImg2Oss(fileImg[i], Constants.PRODUCT) + ";";
                }
                shoppingProduct.setImg(fileUrl.substring(0, fileUrl.length() - 1));
            }
            shoppingProduct.setCreateName(ShiroUtils.getLoginName());
            return toAjax(shoppingProductService.updateShoppingProduct(shoppingProduct));
        } catch (Exception e) {
            LOGGER.error("修改商品失败！", e);
            return error("修改商品失败！");
        }
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("system:shoppingProduct:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(shoppingProductService.deleteShoppingProductByIds(ids));
    }

}
